// AUSlotsTester.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "AUSlotsTester.h"

#include "MainFrm.h"
#include "ChildFrm.h"
#include "AUSlotsTesterDoc.h"
#include "AUSlotsTesterView.h"
#include "ConfigureWebServerDlg.h"

#include <initguid.h>
#include "AUSlotsTester_i.c"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAUSlotsTesterApp

BEGIN_MESSAGE_MAP(CAUSlotsTesterApp, CWinApp)
	//{{AFX_MSG_MAP(CAUSlotsTesterApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_TEST_CONFIGURE_WEB_SERVER_PORT, OnTestConfigureWebServerPort)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAUSlotsTesterApp construction

CAUSlotsTesterApp::CAUSlotsTesterApp()
{
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CAUSlotsTesterApp object

CAUSlotsTesterApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CAUSlotsTesterApp initialization

BOOL CAUSlotsTesterApp::InitInstance()
{
	/*
	if(! InitATL())
		return FALSE;
	*/
	
	// InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// Set this to include all the common control classes you want to use
	// in your application.
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinApp::InitInstance();
	
	// Initialize OLE libraries
	if (!AfxOleInit())
	{
		AfxMessageBox(IDP_OLE_INIT_FAILED);
		return FALSE;
	}

	AfxEnableControlContainer();

	AfxSocketInit();

	// Change the registry key under which our settings are stored.
	SetRegistryKey(_T("Realtime Gaming"));

	UINT nPort = GetProfileInt( _T("Web Server"), _T("Port"), 80 );

	while ( !m_WebServer.Initialize( nPort ) )
	{
		CConfigureWebServerDlg dlg;

		dlg.SetPort( nPort );

		if ( dlg.DoModal() == IDCANCEL )
		{
			return FALSE;
		}

		nPort = dlg.GetPort();
	}

	WriteProfileInt( _T("Web Server"), _T("Port"), GetWebPort() );

	m_WebPage.SetFilePath( _T( "/" ) );

	m_WebServer.AddWebFile( &m_WebPage );
	m_WebServer.AddWebFileAlias( &m_WebPage, _T( "/default.html" ) );

	// Standard initialization

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register document templates

	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(
		IDR_AUSLOTTYPE,
		RUNTIME_CLASS(CAUSlotsTesterDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CAUSlotsTesterView));

	if (!pDocTemplate)
		return FALSE;

	AddDocTemplate(pDocTemplate);

	// create main MDI Frame window
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
	{
		delete pMainFrame;
		return FALSE;
	}
	m_pMainWnd = pMainFrame;

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated)
	{
		return TRUE;
	}

	pMainFrame->ShowWindow(m_nCmdShow);
	pMainFrame->UpdateWindow();

    return TRUE;
}

UINT CAUSlotsTesterApp::GetWebPort() const
{
	return m_WebServer.GetPort();
}

void CAUSlotsTesterApp::SetWebPageDirty()
{
	m_WebPage.SetDirty( true );
}

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
		// No message handlers
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CAUSlotsTesterApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CAUSlotsTesterApp message handlers


	
CAUSlotsTesterModule _Module;

BEGIN_OBJECT_MAP(ObjectMap)
END_OBJECT_MAP()

LONG CAUSlotsTesterModule::Unlock()
{
	AfxOleUnlockApp();
	return 0;
}

LONG CAUSlotsTesterModule::Lock()
{
	AfxOleLockApp();
	return 1;
}
LPCTSTR CAUSlotsTesterModule::FindOneOf(LPCTSTR p1, LPCTSTR p2)
{
	while (*p1 != NULL)
	{
		LPCTSTR p = p2;
		while (*p != NULL)
		{
			if (*p1 == *p)
				return CharNext(p1);
			p = CharNext(p);
		}
		p1++;
	}
	return NULL;
}


int CAUSlotsTesterApp::ExitInstance()
{
	if (m_bATLInited)
	{
		_Module.RevokeClassObjects();
		_Module.Term();
		CoUninitialize();
	}

	return CWinApp::ExitInstance();

}

BOOL CAUSlotsTesterApp::InitATL()
{
	m_bATLInited = TRUE;

#if _WIN32_WINNT >= 0x0400
	HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
#else
	HRESULT hRes = CoInitialize(NULL);
#endif

	if (FAILED(hRes))
	{
		m_bATLInited = FALSE;
		return FALSE;
	}

	_Module.Init(ObjectMap, AfxGetInstanceHandle());
	_Module.dwThreadID = GetCurrentThreadId();

	LPTSTR lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
	TCHAR szTokens[] = _T("-/");

	BOOL bRun = TRUE;
	LPCTSTR lpszToken = _Module.FindOneOf(lpCmdLine, szTokens);
	while (lpszToken != NULL)
	{
		if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
		{
			_Module.UpdateRegistryFromResource(IDR_AUSLOTSTESTER, FALSE);
			_Module.UnregisterServer(TRUE); //TRUE means typelib is unreg'd
			bRun = FALSE;
			break;
		}
		if (lstrcmpi(lpszToken, _T("RegServer"))==0)
		{
			_Module.UpdateRegistryFromResource(IDR_AUSLOTSTESTER, TRUE);
			_Module.RegisterServer(TRUE);
			bRun = FALSE;
			break;
		}
		lpszToken = _Module.FindOneOf(lpszToken, szTokens);
	}

	if (!bRun)
	{
		m_bATLInited = FALSE;
		_Module.Term();
		CoUninitialize();
		return FALSE;
	}

	hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, 
		REGCLS_MULTIPLEUSE);
	if (FAILED(hRes))
	{
		m_bATLInited = FALSE;
		CoUninitialize();
		return FALSE;
	}	

	return TRUE;
}

void CAUSlotsTesterApp::OnTestConfigureWebServerPort() 
{
	CConfigureWebServerDlg dlg;

	UINT nPort = GetProfileInt( _T("Web Server"), _T("Port"), 80 );

	dlg.SetPort( nPort );

	if ( dlg.DoModal() == IDOK && nPort != dlg.GetPort() )
	{
		WriteProfileInt( _T("Web Server"), _T("Port"), dlg.GetPort() );

		AfxMessageBox( _T("The new port will be used the next time the application is started."), MB_OK );
	}
}

CWebServer& GetWebServer()
{
	return theApp.m_WebServer;
}

BOOL CAUSlotsTesterApp::OnIdle( LONG lCount )
{
	POSITION pos = GetFirstDocTemplatePosition();

	CDocTemplate* pdt = GetNextDocTemplate( pos );

	pos = pdt->GetFirstDocPosition();

	while ( pos )
	{
		CAUSlotsTesterDoc* pDoc = (CAUSlotsTesterDoc*)pdt->GetNextDoc( pos );

		pDoc->OnIdle();
	}

	return CWinApp::OnIdle(lCount);
}
